package com.lagou.service.impl;

import com.lagou.dao.RoleMapper;
import com.lagou.domain.*;
import com.lagou.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    public List<Role> findAllRole(Role role) {
        List<Role> allRole = roleMapper.findAllRole(role);
        return allRole;
    }

    @Override
    public List<Integer> findMenuByRoleId(Integer id) {
        List<Integer> menuByRoleId = roleMapper.findMenuByRoleId(id);
        return menuByRoleId;
    }

    /*为角色分配菜单*/
    public void roleContextMenu(RoleMenuVo roleMenuVo) {
        //1.清空中间表的关联关系
        roleMapper.deleteRoleContextMenu(roleMenuVo.getRoleId());

        //2.为角色分配菜单，通过for循环
        Role_menu_relation rmr = new Role_menu_relation();
        for (Integer mid : roleMenuVo.getMenuIdList()) {
            rmr.setMenuId(mid);
            rmr.setRoleId(roleMenuVo.getRoleId());
            //封装数据
            Date data = new Date();
            rmr.setCreatedTime(data);
            rmr.setUpdatedTime(data);

            rmr.setCreatedBy("system");
            rmr.setUpdatedby("system");
            //执行插入
            roleMapper.roleContextMenu(rmr);
        }
    }

    //删除角色
    @Override
    public void deleteRole(Integer roleid) {

        //调用根据roleid清空中间表关联关系
        roleMapper.deleteRoleContextMenu(roleid);
        //删除角色
        roleMapper.deleteRole(roleid);
    }


    /*修改角色&*/
    @Override
    public void saveRoles(Role role) {
        Date date = new Date();
        role.setCreatedTime(date);
        role.setUpdatedTime(date);
        role.setCreatedBy("system");
        role.setUpdatedBy("system");
        roleMapper.saveRole(role);
    }

    /*添加角色*/
    public void updateRoles(Role role) {
        Date date = new Date();
        role.setUpdatedTime(date);
        role.setUpdatedBy("system");
        roleMapper.updateRole(role);
    }

    /*获取当前角色拥有的资源信息*/
    @Override
    public List<ResourceCategory> findResourceListByRoleId(Integer roleId) {
        List<ResourceCategory> resourceCategories = roleMapper.finResourceCategoryById(roleId);
        return resourceCategories;
    }

    /*为角色分配资源*/
    @Override
    public void roleContextResource(RoleResourceVO roleResourceVO) {
        //1.清空中间表的关联关系
        roleMapper.deleteRoleResourceRelation(roleResourceVO.getRoleId());

        for (Integer rr : roleResourceVO.getResourceIdList()) {
            //2.为角色分配菜单，通过for循环
            RoleResourceRelation roleResourceRelation = new RoleResourceRelation();
            //封装数据
            Date data = new Date();
            roleResourceRelation.setCreatedTime(data);
            roleResourceRelation.setUpdatedTime(data);
            roleResourceRelation.setCreatedBy("system");
            roleResourceRelation.setUpdatedby("system");
            roleResourceRelation.setRoleId(roleResourceVO.getRoleId());
            roleResourceRelation.setResourceId(rr);
            //执行插入
            roleMapper.roleContextResource(roleResourceRelation);
        }
    }
}
